﻿using EstimatesTool.Properties;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace EstimatesTool.Util;

public class CommentUtil(List<string> Ext)
{
    private readonly List<string> Exts = Ext;

    private readonly Dictionary<string, string> Single = [];

    private readonly Dictionary<string, string> Multi = [];

    private readonly List<string> CoBol = [".cbl", ".cob"];

    public void Init()
    {
        var xml = Resources.Comment;
        var root = new XmlDocument();
        root.LoadXml(xml);
        foreach (var ext in Exts)
        {
            var single = root.SelectNodes($"//SingLine/Item/Ext[text()='{ext}']");
            var sub = new List<string>();
            foreach (XmlNode node in single!)
            {
                sub.Add(node.ParentNode!.Attributes!["Txt"]!.Value);
            }
            Single[ext] = string.Join(",", sub);
            sub = [];
            var multi = root.SelectNodes($"//MultiLine/Item/Ext[text()='{ext}']");
            foreach (XmlNode node in multi!)
            {
                var start = node.ParentNode!.Attributes!["Start"]!.Value;
                var end = node.ParentNode!.Attributes!["End"]!.Value;
                sub.Add($"{start}|{end}");
            }
            Multi[ext] = string.Join(",", sub);
        }
    }

    public int GetCount(string ext, List<string> lines)
    {
        Single.TryGetValue(ext, out string? single);
        if (string.IsNullOrEmpty(single)) return lines.Count;
        if (CoBol.Contains(ext))
        {
            lines = lines.Select(s => Regex.Replace(s, "^[0-9]+\\s", "")).ToList();
        }
        var arr = single.Split('|');
        foreach (var item in arr)
        {
            lines = lines.Where(s => !s.StartsWith(item)).ToList();
        }
        Multi.TryGetValue(ext, out string? mutil);
        if (string.IsNullOrEmpty(mutil)) return lines.Count;
        else
        {
            var dic = new Dictionary<string, string>();
            foreach (var item in mutil.Split(","))
            {
                arr = item.Split('|');
                dic[arr[0]] = arr[1];
            }
            return Total(dic, lines);
        }
    }
    private int Total(Dictionary<string, string> dic, List<string> lines)
    {
        int num = 0;
        var sin = Single.Values.ToList();
        for (int i = 0; i < lines.Count; i++)
        {
            var line = lines[i];
            var key = dic.Keys.FirstOrDefault(k => line.Contains(k));
            if (key != null)
            {
                var val = dic[key];
                var real = Regex.Replace(line, $"\\b{key}.*", "").Trim();
                if (!string.IsNullOrEmpty(real) && !sin.Any(real.StartsWith)) num++;
                while (line.EndsWith(val))
                {
                    line = lines[++i];
                }
                real = Regex.Replace(real, $".*?\\b{val}", "").Trim();
                if (!string.IsNullOrEmpty(real) && !sin.Any(s => real.StartsWith(s))) num++;
                continue;
            }
            num++;
        }
        return num;
    }
}
